#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

class Solution
{
public:
    static int minOperations(vector<int>& nums, int x)
    {
        int left = 0;
        int right = 0;
        int len = 0;
        int sum = 0;
        int target = accumulate(nums.begin(), nums.end(), 0) - x;
        if(target < 0)
            return -1;
        if(target == 0)
            return nums.size();
        while(right < nums.size())
        {
            sum += nums[right];
            while(sum > target)
            {
                sum -= nums[left++];
            }
            if(sum == target)
            {
                len = max(len, right-left+1);
            }
            right++;
        }
        if(len)
            return nums.size()-len;
        else
            return -1;
    }
};